package com.hjm.admin.moule.viewmodel.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.utils.ToastUtils;
import com.hjm.admin.moule.api.UserService;
import com.hjm.admin.moule.db.entity.UserBean;
import com.hjm.admin.moule.db.helpter.UserHelper;
import com.hjm.admin.moule.utils.SharedPreUtils;
import com.hjm.admin.moule.utils.rxhelper.RxObserver;
import com.hjm.admin.moule.view.activity.impl.MainActivity;
import com.hjm.admin.moule.viewmodel.BaseViewModel;


public class VMUseLoginInfo extends BaseViewModel {

    public VMUseLoginInfo(Context mContext) {
        super(mContext);
    }

    public void login(String userName, String password) {
        RxHttpUtils.getSInstance().addHeaders(tokenMap()).createSApi(UserService.class)
                .login(userName, password)
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<UserBean>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }
                    @Override
                    protected void onSuccess(UserBean data) {
                        ToastUtils.showToast("登陆成功");
                        UserHelper.getsInstance().saveUser(data);
                        SharedPreUtils.getInstance().putString("token", data.getToken());
                        SharedPreUtils.getInstance().putString("username", data.name);
                        mContext.startActivity(new Intent(mContext, MainActivity.class));
                        ((Activity) mContext).finish();
                    }
                });
    }
}
